package com.example.administrator.myapplication.find.recommend;


import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.util.Pair;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.administrator.myapplication.BR;
import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.callback.XmlyService;
import com.example.administrator.myapplication.databinding.FragmentRecommendBinding;
import com.example.administrator.myapplication.find.recommend.bean.recommendEntity.DiscoveryColumns;
import com.example.administrator.myapplication.find.recommend.bean.recommendEntity.EditorRecommendAlbums;
import com.example.administrator.myapplication.find.recommend.bean.recommendEntity.FocusImages;
import com.example.administrator.myapplication.find.recommend.bean.recommendEntity.RecommendEntity;
import com.example.administrator.myapplication.find.recommend.bean.recommendEntity.SpecialColumn;
import com.example.administrator.myapplication.find.recommend.bean.recommendhotEntity.HotRecommendEntity;
import com.example.administrator.myapplication.find.recommend.bean.recommendhotEntity.RecommendTitle;
import com.example.administrator.myapplication.until.BaseApplication;
import com.example.administrator.myapplication.until.CommentRecyclerAdapter;

import java.util.ArrayList;
import java.util.HashMap;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class RecommendFragment extends Fragment implements Callback<RecommendEntity> {
     private   FragmentRecommendBinding binding;
    private CommentRecyclerAdapter<Object> adapter;
    private XmlyService service;

    public RecommendFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return DataBindingUtil.inflate(inflater,R.layout.fragment_recommend, container, false).getRoot();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        binding = DataBindingUtil.getBinding(view);
        HashMap<Class<?>,Pair<Integer,Integer>> map = new HashMap<>();
        map.put(FocusImages.class,Pair.create(R.layout.recommend_focus_images,BR.focusImages2));
        map.put(DiscoveryColumns.class,Pair.create(R.layout.recommend_discover_images,BR.discoveryImages));
        map.put(EditorRecommendAlbums.class,Pair.create(R.layout.recommend_editorrecommends,BR.editorRecommendAlbums));
        map.put(SpecialColumn.class,Pair.create(R.layout.recommend_specialcolumns,BR.specialColumn));
        map.put(RecommendTitle.class,Pair.create(R.layout.recommend_hot_recommends,BR.recommendTitle));
        adapter = new CommentRecyclerAdapter<>(getContext(),new ArrayList<>(), map);
        binding.recycler.setAdapter(adapter);
        service = ((BaseApplication) getContext().getApplicationContext()).getService();
        service.getRecommendEntity().enqueue(this);

    }

    @Override
    public void onResponse(Call<RecommendEntity> call, Response<RecommendEntity> response) {
        adapter.add(0,response.body().getFocusImages());
        adapter.add(1,response.body().getDiscoveryColumns());
        adapter.add(2,response.body().getEditorRecommendAlbums());
        adapter.add(3,response.body().getSpecialColumn());
        aboutHotRecommend();
    }

    private void aboutHotRecommend() {
        service.getHotRecommendEntity().enqueue(new Callback<HotRecommendEntity>() {
            @Override
            public void onResponse(Call<HotRecommendEntity> call, Response<HotRecommendEntity> response) {
                adapter.addAll(response.body().getHotRecommends().getList());
            }

            @Override
            public void onFailure(Call<HotRecommendEntity> call, Throwable t) {
                Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onFailure(Call<RecommendEntity> call, Throwable t) {
        t.getMessage();
        Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
    }
}
